﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Windows.Forms;

namespace StudentClubMisWf
{
    public partial class frmAddClubUser : Form
    {
        public frmAddClubUser()
        {
            InitializeComponent();
        }

        private void frmAddClubUser_Load(object sender, EventArgs e)
        {
            cmbRole.SelectedIndex = 1;
        }

        private void btnAddClubUser_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtUserName.Text) || string.IsNullOrEmpty(txtPassword1.Text) || string.IsNullOrEmpty(txtPassword2.Text))
            {
                MessageBox.Show("用户名和密码不能为空！");
            }
            else if (!txtPassword2.Text.Equals(txtPassword1.Text))
            {
                MessageBox.Show("两次输入的密码必须一致）");
            }
            else
            {
                ClubUserBLL clubUserBLL = new ClubUserBLL();
                ClubUser clubUser = new ClubUser();
                clubUser.UserName = txtUserName.Text;
                clubUser.Password = txtPassword2.Text;
                clubUser.Role = cmbRole.Text;

                string message = clubUserBLL.AddClubUser(clubUser) > 0 ? "添加用户成功！" : "添加用户失败！";
                DialogResult r = MessageBox.Show(message, "是否继续添加？", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (r == DialogResult.No)
                {
                    this.Close();
                    //Thread th = new(delegate()
                    //{
                    //    frmClubUser f = new();
                    //    f.SetData();
                        
                    //    f.Text = txtUserName.Text;
                    //    f.lsbUserName.DataBindings.Clear();
                    //});
                    //th.Start();
                    
                }
                else
                {
                    ResetInputText();
                }

            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            ResetInputText();
        }
        private void ResetInputText()
        {
            txtUserName.Text = "";
            txtPassword1.Text = txtPassword2.Text = "";
        }
    }
}
